#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

#region Change Log
//  
//  Commit: 11/11/2008 15:57:22 - SN118662190318\Stéphan Leclercq
//    Change: 17B18B1B-07ED-4CCF-B12A-5675042F584C: Miscellaneous Bugs & Enhancements
//    Reviewed: 11/11/2008 15:57:21 - SN118662190318\Stéphan Leclercq
//  
#endregion


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Streambolics
{
    /// <summary>
    ///     A replacement for Delphi's TStringList
    /// </summary>
    /// <remarks><para>
    ///     A StringList is mainly a collection of strings each representing
    ///     a "line" of a text.
    /// </para><para>
    ///     While closely resembling a Delphi's TStringList, the StringList has
    ///     some special characteristics. Most notably, setting carriage returns or
    ///     line feeds in one of the items will not "split" the item in two as does
    ///     the TStringList.
    /// </para></remarks>

    public class StringList : List<String>
    {
        /// <summary>
        ///     Creates a new, empty list.
        /// </summary>

        public StringList ()
            : base ()
        {
        }

        /// <summary>
        ///     Creates a new list based on the lines in the string.
        /// </summary>
        /// <param name="aText">
        ///     The initial contents.
        /// </param>

        public StringList (string aText)
            : base ()
        {
            Text = aText;
        }

        /// <summary>
        ///     Save as a text to the file with the given name.
        /// </summary>
        /// <param name="aFileName">
        ///     The name of the file to save to.
        /// </param>

        public void SaveToFile (string aFileName)
        {
            //File.WriteAllLines (aFileName, this);
        }

        /// <summary>
        ///     Loads the list from a text file.
        /// </summary>
        /// <param name="aFileName">
        ///     The name of the file to load.
        /// </param>

        public void LoadFromFile (string aFileName)
        {
            using (Stream s = File.OpenRead (aFileName))
            {
                LoadFromStream (s);
            }
        }

        /// <summary>
        ///     Loads the list from a stream.
        /// </summary>
        /// <param name="s">
        ///     The stream to read.
        /// </param>

        public void LoadFromStream (Stream s)
        {
            LoadFromReader (new StreamReader (s));
        }

        /// <summary>
        ///     Loads the list from a TextReader.
        /// </summary>
        /// <param name="r">
        ///     The TextReader to read from.
        /// </param>

        public void LoadFromReader (TextReader r)
        {
            Clear ();
            string l;

            while ((l = r.ReadLine ()) != null)
            {
                Add (l);
            }
        }

        /// <summary>
        ///     The list as a single string.
        /// </summary>

        public string Text
        {
            get
            {
                StringBuilder t = new StringBuilder();
                foreach (string l in this)
                {
                    t.AppendLine (l);
                }
                return t.ToString();
            }
            set
            {
                LoadFromReader (new StringReader (value));
            }
        }

        /// <summary>
        ///     Redefinded to return Text.
        /// </summary>
        /// <returns>
        ///     Same as the Text property.
        /// </returns>

        public override string ToString ()
        {
            return Text;
        }

        /// <summary>
        ///     Saves the contents as lines into a stream.
        /// </summary>
        /// <param name="s">
        ///     The stream to write to.
        /// </param>

        public void SaveToStream (Stream s)
        {
            StreamWriter sw = new StreamWriter (s);
            foreach (string l in this)
            {
                sw.WriteLine (l);
            }
            sw.Flush ();
        }

        /// <summary>
        ///     Prefix all strings with p.
        /// </summary>
        /// <param name="p">
        ///     The prefix to be added to each string.
        /// </param>

        public void PrefixWith (string p)
        {
            for (int i = 0; i < Count; i++)
            {
                this[i] = p + this[i];
            }
        }

        /// <summary>
        ///     Whether the argument contains all strings.
        /// </summary>
        /// <param name="p">
        ///     The string to test.
        /// </param>
        /// <returns>
        ///     True if all strings are contained in p, false otherwise.
        /// </returns>

        public bool AllIn (string p)
        {
            foreach (string s in this)
            {
                if (!p.Contains (s))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        ///     Whether the argument contains at least one of the strings of
        ///     the list.
        /// </summary>
        /// <param name="p">
        ///     The string to test.
        /// </param>
        /// <returns>
        ///     True if at least one of the strings in the list is contained in
        ///     p. False otherwise.
        /// </returns>

        public bool AnyIn (string p)
        {
            foreach (string s in this)
            {
                if (p.Contains (s))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
